import { t } from 'i18next'
import Alert from '@/components/Alert'
import requestMeeting, { request } from '@/plugins/axios'
import { Meeting } from '../meeting'
import { Mt } from '../mt'
import { DropDataMt, IMcc, makeMtEntityParam, makeMtParam } from '../type'

export const fetchCallMt = async (meetingMoid: string, params: { cascade_id?: string; mts: any }) => {
	request({
		url: `/mcc/${meetingMoid}/mts`,
		method: 'post',
		data: params,
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('呼叫终端失败'))
			}
		})
		.catch(() => {
			Alert(t('呼叫终端失败'))
		})
}
/** 终端互动席观众席切换*/
export const fetchInteractive = async (meeting_moid: string, mt: Mt) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/mts/${mt.mtid}/switch`,
		method: 'PUT',
		data: { value: mt.status.isInteractive ? 2 : 1, mt: makeMtParam(mt) },
	})
		.then((res) => {
			if (!res.success) {
				if (mt.status.isInteractive) {
					Alert(t('取消互动终端失败'))
				} else {
					Alert(t('设置互动终端失败'))
				}
			}
		})
		.catch(() => {
			if (mt.status.isInteractive) {
				Alert(t('取消互动终端失败'))
			} else {
				Alert(t('设置互动终端失败'))
			}
		})
/** 终端哑音操作*/
export const fetchMute = async (type: boolean, meeting_moid: string, mt: IMcc) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/mts/mute`,
		method: 'PUT',
		data: { value: type ? 0 : 1, mts: [makeMtParam(mt)] },
	})
		.then((res) => {
			if (!res.success) {
				if (type) {
					Alert(t('取消哑音失败'))
				} else {
					Alert(t('设置哑音失败'))
				}
			}
		})
		.catch(() => {
			if (type) {
				Alert(t('取消哑音失败'))
			} else {
				Alert(t('设置哑音失败'))
			}
		})
/** 终端静音操作*/
export const fetchSilence = async (type: boolean, meeting_moid: string, mt: IMcc) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/mts/silence`,
		method: 'PUT',
		data: { value: type ? 0 : 1, mts: [makeMtParam(mt)] },
	})
		.then((res) => {
			if (!res.success) {
				if (type) {
					Alert(t('取消静音失败'))
				} else {
					Alert(t('设置静音失败'))
				}
			}
		})
		.catch(() => {
			if (type) {
				Alert(t('取消静音失败'))
			} else {
				Alert(t('设置静音失败'))
			}
		})
/** 终端发起双流*/
// debugger
export const fetchStartDoubleFlow = async (meeting_moid: string, mt: IMcc) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/dualstream`,
		method: 'PUT',
		data: makeMtParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('发起内容共享失败'))
			}
		})
		.catch(() => {
			Alert(t('发起内容共享失败'))
		})
/** 取消双流*/
export const fetchStopDoubleFlow = async (meeting_id: string, mt: IMcc) =>
	requestMeeting({
		url: `/mcc/${meeting_id}/dualstream`,
		method: 'DELETE',
		data: makeMtParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('取消内容共享失败'))
			}
		})
		.catch(() => {
			Alert(t('取消内容共享失败'))
		})
/** 设置管理方*/
export const fetchSetChairman = async (meeting_moid: string, mt: DropDataMt | Mt) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/chairman`,
		method: 'PUT',
		data: mt instanceof Mt ? makeMtParam(mt) : makeMtEntityParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				let message = res.description || t('设置管理方失败')
				if (message.match('xxx')) {
					// 若需要弹出的消息中包含'xxx',替换成终端的名字
					Alert(message.replace(/xxx/, mt.name || ''))
					return
				}
				Alert(message)
			}
		})
		.catch(() => {
			Alert(t('设置管理方失败'))
		})
/** 取消管理方*/
export const fetchCancelChairman = async (meeting_moid: string, mt: Mt) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/chairman`,
		method: 'DELETE',
		data: makeMtParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('取消管理方失败'))
			} else {
				return res
			}
		})
		.catch(() => {
			Alert(t('取消管理方失败'))
		})
/** 取消会议发言方*/
export const fetchCancelSpeaker = async (meeting_moid: string, mt: IMcc) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/speaker`,
		method: 'DELETE',
		data: makeMtParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('取消发言方失败'))
			} else {
				return res
			}
		})
		.catch(() => {
			Alert(t('取消发言方失败'))
		})
/** 指定会议发言人*/
export const fetchSetSpeaker = async (meeting_moid: string, mt: DropDataMt | IMcc) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/speaker`,
		method: 'PUT',
		data: {
			force_brd: 0,
			mt: mt instanceof Meeting || mt instanceof Mt ? makeMtParam(mt) : makeMtEntityParam(mt),
		},
	})
		.then((res) => {
			if (!res.success) {
				let message = res.description || t('设置发言方失败')
				Alert(message)
			}
		})
		.catch(() => {
			Alert(t('设置发言方失败'))
		})
/** 设置终端强制广播*/
/**force_broadcast:是否设置发言人强制广播，仅SFU会议有效，默认为0  0-否;  1-是; */
export const fetchForceBrd = async (meeting_moid: string, mt: DropDataMt | IMcc, force_brd: boolean) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/speaker`,
		method: 'PUT',
		data: {
			force_brd: force_brd ? 1 : 0,
			mt: mt instanceof Meeting || mt instanceof Mt ? makeMtParam(mt) : makeMtEntityParam(mt),
		},
	})
		.then((res) => {
			if (!res.success) {
				let message = res.description || (force_brd ? t('设置强制广播失败') : t('取消强制广播失败'))
				Alert(message)
			}
		})
		.catch(() => {
			Alert(force_brd ? t('设置强制广播失败') : t('取消强制广播失败'))
		})
/** 删除终端*/
export const fetchDeleteMt = async (meeting_moid: string, mts: IMcc[]) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/mts`,
		method: 'DELETE',
		data: {
			mts: mts.map((x) => {
				let interactive_mt = x instanceof Mt ? ~~x.status.isInteractive : 1
				return {
					...makeMtParam(x),
					interactive_mt,
				}
			}),
		},
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('删除终端失败'))
			}
		})
		.catch(() => {
			Alert(t('删除终端失败'))
		})
/** 设置常用终端 */
export const fetchSetOftenMt = async (meeting_moid: string, mt: Mt) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/often_mts`,
		method: 'POST',
		data: makeMtParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('设置常用终端失败'))
			}
		})
		.catch(() => {
			Alert(t('设置常用终端失败'))
		})
/** 取消常用终端*/
export const fetchDeleteOftenMt = async (meeting_moid: string, mt: Mt) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/often_mts`,
		method: 'DELETE',
		data: makeMtParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('取消常用终端'))
			}
		})
		.catch(() => {
			Alert(t('取消常用终端'))
		})
/** 呼叫终端 */
export const fetchCallOnlineMt = async (meeting_moid: string, mts: IMcc[]) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/online_mts`,
		method: 'POST',
		data: {
			mts: mts.map((x) => {
				let interactive_mt = x instanceof Mt ? ~~x.status.isInteractive : 1
				return {
					...makeMtParam(x),
					interactive_mt,
				}
			}),
		},
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('呼叫终端失败'))
			}
		})
		.catch(() => {
			Alert(t('呼叫终端失败'))
		})
/** 挂断终端*/
export const fetchDeleteOnlineMt = async (meeting_moid: string, mts: IMcc[]) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/online_mts`,
		method: 'DELETE',
		data: {
			mts: mts.map((x) => {
				let interactive_mt = x instanceof Mt ? ~~x.status.isInteractive : 1
				return {
					...makeMtParam(x),
					interactive_mt,
				}
			}),
		},
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('挂断终端失败'))
			}
		})
		.catch(() => {
			Alert(t('挂断终端失败'))
		})
/** 设置上传终端 */
export const fetchSetUpMt = async (meeting_moid: string, mt: IMcc) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/upload`,
		method: 'PUT',
		data: makeMtParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('上传失败'))
			}
		})
		.catch(() => {
			Alert(t('上传失败'))
		})
/** 取消上传终端 */
export const fetchCancelUpMt = async (meeting_moid: string, mt: IMcc) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/upload`,
		method: 'delete',
		data: makeMtParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				Alert(res.description || t('取消上传失败'))
			}
		})
		.catch(() => {
			Alert(t('取消上传失败'))
		})
/** 拒绝申请互动发言*/
export const fetchRejectDiscussApply = async (meeting_moid: string, mt: IMcc) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/mts/${mt.mtKey}/apply_discuss`,
		method: 'DELETE',
		data: makeMtParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('拒绝{{name}}申请互动发言失败', { name: mt.name ? mt.name : '' }))
			}
		})
		.catch(() => {
			Alert(t('拒绝{{name}}申请互动发言失败', { name: mt.name ? mt.name : '' }))
		})
/** 强制广播 flag 1-关闭 0-开启 */
export const fetchForceBroadcast = async (meeting_moid: string, flag: 0 | 1) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/force_broadcast`,
		method: flag ? 'delete' : 'post',
	})
		.then((res) => {
			if (!res.success) {
				flag ? Alert(t('取消强制广播失败')) : Alert(t('强制广播失败'))
			}
		})
		.catch(() => {
			flag ? Alert(t('取消强制广播失败')) : Alert(t('强制广播失败'))
		})
/** 呼叫全部离线与会方 */
export const fetchCallAllOfflineMt = async (url: string) =>
	requestMeeting({
		url: url,
		method: 'POST',
	}).then((res) => res)
/** 全场哑音 */
export const fetchAllMute = async (meeting_moid: string, force_mute: 0 | 1) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/all_mute`,
		method: 'POST',
		data: { force_mute: force_mute },
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('设置全场哑音失败'))
			}
		})
		.catch((err) => Alert(t('设置全场哑音失败')))
/** 取消全场哑音*/
export const fetchCancelAllMute = async (meeting_moid: string) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/all_mute`,
		method: 'DELETE',
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('取消全场哑音失败'))
			}
		})
		.catch(() => {
			Alert(t('取消全场哑音失败'))
		})
/** 全场静音 */
export const fetchAllSilence = async (meeting_moid: string) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/all_silence`,
		method: 'POST',
		data: { is_mt_mute: 0 },
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('设置全场静音失败'))
			}
		})
		.catch((err) => Alert(t('设置全场静音失败')))
/** 取消全场静音*/
export const fetchCancelAllSilence = async (meeting_moid: string) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/all_silence`,
		method: 'DELETE',
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('取消全场静音失败'))
			}
		})
		.catch(() => {
			Alert(t('取消全场静音失败'))
		})
/** 管理方选看
 * mtid: 主席mtid
 * flag 0-关闭管理方选看  1-开启管理方选看
 */
export const fetchChairmanVmp = async (meeting_moid: string, mtid: string, flag: 0 | 1) =>
	requestMeeting({
		url: `/mcc/${meeting_moid}/select_view/chairman/${mtid}`,
		method: flag ? 'POST' : 'DELETE',
	})
		.then((res) => {
			if (!res.success) {
				flag ? Alert(t('开启管理方选看失败')) : Alert(t('取消管理方选看失败'))
			}
		})
		.catch(() => {
			flag ? Alert(t('开启管理方选看失败')) : Alert(t('取消管理方选看失败'))
		})

/**
 *
 * @param meetingMoid 会议的meetingMoid
 * @returns 强制哑音
 */
export const fetchForceMute = async (meetingMoid: string, param: { value: 0 | 1 }) =>
	requestMeeting({
		url: `/mcc/${meetingMoid}/force_mute`,
		method: 'POST',
		data: JSON.stringify(param),
	}).then((res) => res)
